<?php
class userModel extends appModel{
    
    protected function init(){
        
    } 

    public function read($id=false){       
        $data = array();
        $q = "";
        if(!empty($id)){
            $q = "WHERE `id`={$id}";
        }
        $start = isset($_GET['start']) ? $_GET['start'] : 0;
        $limit = isset($_GET['limit']) ? $_GET['limit'] : 10;
        $query = "SELECT * FROM `users` {$q} ORDER BY `username` DESC LIMIT {$start},{$limit}";

        $result = $this->db->query($query);
        
        $nbrows = $this->db->fetchRow($this->db->query("SELECT COUNT(*) FROM `users`"));
        $nbrows = $nbrows[0];
        while($row=$this->db->fetchAssoc($result)){
            $data[]=$row;
        }
// print_r($data);
        if(MyHelpers::isAjax()){
            // prepare the metadata
            $ret['metaData'] =   $this->makeMetaData($result);
            $ret['total']    =   $nbrows;
            $ret['data']     =   $this->Char2Utf8($data);
            $ret['success']  =   true;
            $ret['message']  =   true;
            return json_encode($ret);
        }
        return $data;       
    }
    
    public function login ($username,$password){
        $id = false;
        
        $result = $this->db->query("SELECT *  FROM users WHERE username='$username' and password='$password' limit 0, 1");
       
        if($result != null){
            while($row=$this->db->fetchAssoc($result)){

                $id = $row['id'];
                $username = $row['username'];
                $password = $row['password'];
            }
        } 

        return $id;
    }
    public function signup($username,$password,$email){
        $id = false;
        
        $result = $this->db->query("INSERT INTO users (username, password, email) VALUES ('$username', '$password', '$email')");
        if($result != false){
            $id = $this->db->insertId();
        }

        return $id;
    }
    public function createuserprofile($Fname, $Lname, $Mname, $Phone, $Country, $State, $City, $Discrp, $Role, $Active, $Confirm) {
        $id = false;
        $result = $this->db->query("INSERT INTO userprofile (user_id, first_name, last_name, middel_name, phone, country, state, city, descriptions) VALUES ('$Fname', '$Lname', '$Mname', '$Phone', '$Country', '$State', '$City', '$Discrp', '$Role', '$Active', '$Confirm')");
        if($result != false){
            $id = $this->db->insertId();
        }           
        return $id;
    }
    public function chk_user_exist($username){
        $id = false;
        $result = $this->db->query("SELECT *  FROM users WHERE username='$username' limit 0, 1");
        if($result != false){
            while($row=$this->db->fetchAssoc($result)){
                $id = $row['id'];
                $username = $row['username'];
                $email = $row['email'];
            }
        } 

        return $id;
    }

 
        //function are userd for add new user
    public function addUser() {
        if (isset($_POST["submit"])) {
            $Uname = $_POST["username"];
            $emial = $_POST["email"];
            $Md5Pass = $_POST["password"];
            $Md5Pass2 = md5($Md5Pass);
            $Pass = md5($Md5Pass2);
            $object = new UserController();
            $object->insertdb($username, $emial, $password);
        }
    }

    public function CheckUserProfile($id) {
        $object = new UserController();
        $result = $object->CheckUserProfiles($id);
        if ($result) {
            return $result;
        }
    }
   
}